package model

import "inventory/internal/constant"

// 检查仓库合法性
func (w Warehouse) CheckEnable(sgId uint32) error {
	if w.Status != constant.WarehouseEnable || w.SgID != sgId {
		return constant.ErrWarehouseNotExist
	}
	return nil
}

// 仓库map
type WarehouseMap map[uint32]*Warehouse

// 初始化map
func (wm WarehouseMap) InitFromWarehouseArray(warehouses []*Warehouse, sgId uint32, checkStatus bool) (err error) {
	for _, warehouse := range warehouses {
		if checkStatus {
			err = warehouse.CheckEnable(sgId)
			if err != nil {
				return
			}
		}
		wm[warehouse.ID] = warehouse
	}
	return
}

// GetKeys
func (wm WarehouseMap) GetKeys() (keys []uint32) {
	keys = make([]uint32, 0, len(wm))
	for key := range wm {
		keys = append(keys, key)
	}
	return
}
